package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.ProdProp;
import cn.com.dcsgo.domain.ProdPropValue;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.dto.ProdPropAddDTO;
import cn.com.dcsgo.dto.ProdPropQueryDTO;
import cn.com.dcsgo.dto.ProdPropUpdateDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.ProdPropService;
import cn.com.dcsgo.utils.AuthUtil;
import cn.com.dcsgo.vo.ProdPropVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格相关接口
 *
 * @author Dcsgo
 * @since 2025/10/18 10:16
 */
@Tag(name = "商品规格相关接口")
@RestController
@RequestMapping("prod/spec")
public class ProdPropController {
    @Resource
    private ProdPropService prodPropService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    @Operation(summary = "分页查询商品规格信息", description = "超级店铺能看到所有商品规格，其他店铺只能看到自己店铺下的商品规格")
    public Result<Page<ProdPropVO>> getProdPropPage(PageDTO pageDTO, ProdPropQueryDTO prodPropQueryDTO) {
        Page<ProdPropVO> result = prodPropService.getPage(pageDTO, prodPropQueryDTO);
        return Result.success(result);
    }

    @GetMapping("list")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    @Operation(summary = "获取所有规格信息",description = "只包含当前用户店铺下的规格信息，超级店铺能看到所有规格")
    public Result<List<ProdProp>> getAllProdProp(){
        return Result.success(prodPropService.getAllProdPropByShopId(AuthUtil.getShopId()));
    }

    @GetMapping("listSpecValue/{propId}")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    @Operation(summary = "获取规格值",description = "根据规格ID获取")
    public Result<List<ProdPropValue>> getProdPropValues(@PathVariable Long propId){
        return Result.success(prodPropService.getPropValues(propId));
    }

    @PostMapping
    @PreAuthorize("hasAuthority('prod:spec:save')")
    @Operation(summary = "添加规格")
    public Result<Void> addProdProp(@Validated @RequestBody ProdPropAddDTO prodPropAddDTO) {
        return Result.handle(prodPropService.save(prodPropAddDTO));
    }

    @PutMapping
    @PreAuthorize("hasAuthority('prod:spec:update')")
    @Operation(summary = "修改规格", description = "规格值列表不为null则对旧的规格值进行覆盖")
    public Result<Void> updateProdProp(@Validated @RequestBody ProdPropUpdateDTO prodPropUpdateDTO) {
        return Result.handle(prodPropService.update(prodPropUpdateDTO));
    }

    @DeleteMapping("{propId}")
    @PreAuthorize("hasAuthority('prod:spec:delete')")
    @Operation(summary = "删除规格", description = "一并删除关联的规格值")
    public Result<Void> deleteProdProp(@PathVariable Long propId) {
        return Result.handle(prodPropService.removePropAndValuesById(propId));
    }
}
